package com.powerbosframework.web.action;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.powerbosframework.web.form.ActionForm;
import com.powerbosframework.web.form.MultipartForm;
import com.powerbosframework.web.util.RequestURI;
import com.powerbosframework.web.util.ValueStack;

/**
 * 鎺у埗鍣ㄥ姩浣滀笂涓嬫枃
 * @author youfu.wang
 * @version 1.0
 */
public class ActionContext {
	private ServletContext servletContext;
	private HttpServletRequest request;
	private HttpServletResponse response;	
	private ActionForm actionForm;
	private boolean multipart = false;	
	private ValueStack vs = new ValueStack();
	
	public ActionContext() {

	}

	public ActionContext(HttpServletRequest request,HttpServletResponse response) {
		this.request = request;
		this.response = response;		
		actionForm=new ActionForm(this.request);
	}
	
	public void processActionForm(SupportAction supportAction){		
		String contentType = request.getContentType();
		if (contentType != null) {
			if (contentType.indexOf("multipart/form-data") != -1) {
				this.multipart = true;
			}
		}	
		try {
			//判断是否多主体表单
			if(this.multipart){
				MultipartForm multipartForm=new MultipartForm(this.request);
				
				multipartForm.processMultipartForm(supportAction);
			}else{
				actionForm.processActionForm(supportAction);
			}
			
		} catch (SecurityException e) {			
			e.printStackTrace();
		} catch (InstantiationException e) {			
			e.printStackTrace();
		} catch (IllegalAccessException e) {			
			e.printStackTrace();
		}
	}
	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}
	
	public HttpSession getSession() {
		return request.getSession();
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public ValueStack getValueStack() {
		return vs;
	}

	public void set(String name, Object value) {
		vs.set(name, value);
	}

	public String get(String name) {
		String value="";
		value=this.actionForm.getParameterValue(name);		 
		return RequestURI.getPostEncode(value);
	}

	public String get(String name, boolean encode) {
		String value="";
		value=this.actionForm.getParameterValue(name);	
		if (encode)
			value = RequestURI.getPostEncode(value);
		return value;
	}

	public String[] getArray(String name) {
		return this.actionForm.getParameterValues(name);
	}
}
